cc.Class({
    extends: cc.Component,
	
	changeSuccess(game){//交换成功 关卡模式减去步数  竞速模式暂停记时
		if(game.playerType==2){
			game.timeCost = false;
		}else{
			game.lastStep--;
		}
	},
	
	getMoveDir(game){//2 -- ud  3 -- uldr  4 -- urdl
		var dir = 2;
		
		if(game.selectedBlock == game.selectedBlock2.mpUL || game.selectedBlock == game.selectedBlock2.mpDR){
			dir=3;
		}else if(game.selectedBlock == game.selectedBlock2.mpUR || game.selectedBlock == game.selectedBlock2.mpDL){
			dir=4;
		}
		return dir;
	},
	
	speAndsep(game){//将两球加入待消除数组
	
		if(
			(game.selectedBlock.isLine()&&!game.selectedBlock2.isLine())||
			(game.selectedBlock2.isLine()&&!game.selectedBlock.isLine())
		){
			
			var lineBlock = game.selectedBlock.isLine()?game.selectedBlock:game.selectedBlock2;
			var notlineBlock = game.selectedBlock.isLine()?game.selectedBlock2:game.selectedBlock;
			
			/*
			if(lineBlock.mpU!=null){
				lineBlock.mpU.type2=lineBlock.type2;
				game.pushMapPointForDisappear(lineBlock.mpU);
			}
			
			game.pushMapPointForDisappear(lineBlock);
			
			if(lineBlock.mpD!=null){
				lineBlock.mpD.type2=lineBlock.type2;
				game.pushMapPointForDisappear(lineBlock.mpD);
			}*/
			
			if(lineBlock.type2==2)
				lineBlock.type2=9;
			else if(lineBlock.type2==3)
				lineBlock.type2=10;
			else if(lineBlock.type2==4)
				lineBlock.type2=11;
			
			notlineBlock.effectFlag=1;
			
			game.pushMapPointForDisappear(lineBlock);		
			
		}else{
			game.pushMapPointForDisappear(game.selectedBlock);		
			game.pushMapPointForDisappear(game.selectedBlock2);
		}
	},
	
	colorAndColor(game){//将两球加入待消除数组 同时将其他所有球变为普通球并加入待消除数组
		game.pushMapPointForDisappear(game.selectedBlock);
		game.selectedBlock.effectFlag=1;		
		game.pushMapPointForDisappear(game.selectedBlock2);
		game.selectedBlock2.effectFlag=1;
		for(var i=0;i<game.mapPointArr.length;i++){
			if(game.mapPointArr[i].type1<=6){
				if(game.pushMapPointForDisappear(game.mapPointArr[i])){
					game.mapPointArr[i].resetType2(1);
				}
			}
		}
	},
	
	colorAndSpe(game){//如果是直线型 则该颜色的所有球变为随机的一种直线球  如果是非直线型则将该颜色所有球变为同类型特殊球
		
		var speBlock = game.selectedBlock.type2==8?game.selectedBlock2:game.selectedBlock;
		var notSpeBlock = game.selectedBlock.type2==8?game.selectedBlock:game.selectedBlock2;
				
		notSpeBlock.type1=speBlock.type1;
				
		game.pushMapPointForDisappear(notSpeBlock);
		game.pushMapPointForDisappear(speBlock);
				
				
		if(speBlock.type2==2||speBlock.type2==3||speBlock.type2==4){
					
			for(var i=0;i<game.mapPointArr.length;i++){
				if(game.mapPointArr[i].type1==speBlock.type1){
					if(game.pushMapPointForDisappear(game.mapPointArr[i])){
						game.mapPointArr[i].resetType2(Math.floor(Math.random()*3)+2);
					}
				}
			}
					
		}else{
			for(var i=0;i<game.mapPointArr.length;i++){
				if(game.mapPointArr[i].type1==speBlock.type1){
					if(game.pushMapPointForDisappear(game.mapPointArr[i])){
						game.mapPointArr[i].resetType2(speBlock.type2);
					}
				}
			}
		}
		
	},
	
	colorAndNormal(game){//将两个球加入待消除数组
		var disappearType1 = (game.selectedBlock.type2==8)?game.selectedBlock2.type1:game.selectedBlock.type1;
			
		var notSpeBlock = game.selectedBlock.type2==8?game.selectedBlock:game.selectedBlock2;
				
		notSpeBlock.type1=disappearType1;
			
		game.pushMapPointForDisappear(game.selectedBlock);
		game.pushMapPointForDisappear(game.selectedBlock2);
	},
	
	normalAndNormal(game,dir){
		
			var total1 = game.selectedBlock.findSame2(dir);
			var total2 = game.selectedBlock2.findSame2(dir);
		
			if(total1.type<=0&&total2.type<=0){//不满足消除条件 还原球
				game.selectedBlock.exchange(game.selectedBlock2,game.exChangeBackCallBack);
			}else{
				
				this.changeSuccess(game);
				
				var total = [];
				if(total1.type>0){//满足消除条件
					total.push(total1.arr);
					if(total1.type!=1){//满足特殊球生成条件
						if(game.selectedBlock.type2==1){//如果本身不是特殊球 则生成特殊球
							game.selectedBlock.resetType2(total1.type);
						}else{//如果本身是特殊球 则直接加入待消除数组
							total.push([[game.selectedBlock]]);
						}
					}
				}
				if(total2.type>0){//满足消除条件
					total.push(total2.arr);
					if(total2.type!=1){//满足特殊球生成条件
						if(game.selectedBlock2.type2==1){//如果本身不是特殊球 则生成特殊球
							game.selectedBlock2.resetType2(total2.type);
						}else{//如果本身是特殊球 则直接加入待消除数组
							total.push([[game.selectedBlock2]]);
						}
					}
				}
			
				game.gameManager.removeSameBlock(total);
				game.disappear();
			}
		
	},

    change(game){
		
		if(game.selectedBlock.type2!=1&&game.selectedBlock2.type2!=1&&game.selectedBlock2.type1<=6&&game.selectedBlock.type1<=6){//特殊球+特殊球 
		
			this.changeSuccess(game);
			
			if(game.selectedBlock.type2!=8&&game.selectedBlock2.type2!=8){//非彩球特殊球+非彩球特殊球
				
				this.speAndsep(game);
				
			}else if(game.selectedBlock.type2==8&&game.selectedBlock2.type2==8){//彩球+彩球
				
				this.colorAndColor(game);
				
			}else{//彩球+特殊球
			
				this.colorAndSpe(game);
				
			}

			game.disappear();
			
		}else if((game.selectedBlock.type2==8&&game.selectedBlock2.type1<=6)||
					(game.selectedBlock2.type2==8&&game.selectedBlock.type1<=6)
				){//彩球+普通
		
			this.changeSuccess(game);
				
			this.colorAndNormal(game);
			
			game.disappear();
				
		}else{//普通球+普通球 或者 普通球+特殊非彩球
		
			var dir = this.getMoveDir(game);
			
			this.normalAndNormal(game,dir);
		}
	}
});
